package cn.itcast.n4;/**
 * @author yd
 * @date 2022/3/18 0:02
 * @version 1.0
 */

import cn.itcast.utils.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

@Slf4j(topic = "w.TestGuardedObject")
public class TestGuardedObject {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 3; i++) {
            new People().start();
        }
        Sleeper.sleep(1);
        for (Integer id : Mailboxes.getIds()) {
            new Postman(id, "内容" + id).start();
        }
    }

}

//业务相关类
@Slf4j(topic = "w.People")
class People extends  Thread{
    @Override
    public void run() {
        //收信
        GuardedObject guardedObject = Mailboxes.createGuardedObject();
        log.debug("开始收信 id {}", guardedObject.getId());
        Object mail = guardedObject.get(50000);
        log.debug("收到信 id: {}, 内容:{}",guardedObject.getId(), mail);
    }
}
@Slf4j(topic = "w.People")
class Postman extends Thread {
    private int id;
    private String mail;
    public Postman(int id, String mail) {
        this.id = id;
        this.mail = mail;
    }
    @Override
    public void run() {
        GuardedObject guardedObject = Mailboxes.getGuardedObject(id);
        log.debug("送信 id:{}, 内容:{}", id, mail);
        guardedObject.complete(mail);
    }
}



//中间解耦类
class Mailboxes{
    private static Map<Integer, GuardedObject> boxes = new Hashtable<>();

    private static int id = 1;
    //产生唯一的id
    private static synchronized int generateId(){
        return  id ++;
    }
    public static  GuardedObject getGuardedObject(int id){
        return boxes.remove(id);
    }

    public static  GuardedObject createGuardedObject(){
        GuardedObject go = new GuardedObject(generateId());
        boxes.put(go.getId(), go);
        return  go;
    }

    public static Set<Integer> getIds(){
        return boxes.keySet();
    }
}




class GuardedObject {
    private int id;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public GuardedObject(int id) {
        this.id = id;
    }

    //结果
    private Object response;

    //获取结果,timeout表示要等待多久
    public Object get(long timeout) {
        synchronized (this) {
            //开始时间
            long begin = System.currentTimeMillis();
            //经历的时间
            long passedTime = 0;
            while (response == null) {
                //这一轮循环应该等待的时间
                long waitTime = timeout - passedTime;
                //经历的时间超过了最大等待时间时，退出循环
                if (timeout - passedTime <= 0) {
                    break;
                }
                try {
                    this.wait(waitTime);//虚假唤醒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //求得经历时间
                passedTime = System.currentTimeMillis() - begin;
            }
            return response;
        }
    }

    //产生结果
    public void complete(Object response) {
        synchronized (this) {
            //给结果成员变量赋值
            this.response = response;
            this.notify();
        }
    }

}